package com.cloudxingkong.dianyinke.NewTeacher.Adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.cloudxingkong.dianyinke.NewTeacher.Ui.ImagePagerActivity;
import com.cloudxingkong.dianyinke.NewTeacher.Utils.NoScrollPFGridView;
import com.cloudxingkong.dianyinke.NewTeacher.bean.StuEvat;
import com.cloudxingkong.dianyinke.R;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by 云星空科技 on 2016/3/18.
 */
public class PingJiaAdapter extends BaseAdapter {
    private List<StuEvat.ListBean> data;
    private Context context;

    public PingJiaAdapter(List<StuEvat.ListBean> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if (convertView == null) {
            convertView=View.inflate(context, R.layout.item_pinjia_listview,null);
            holder=new ViewHolder();
            holder.content= (TextView) convertView.findViewById(R.id.cl_pingjia_content);
            holder.date= (TextView) convertView.findViewById(R.id.cl_pingjia_date);
            holder.ratingBar= (RatingBar) convertView.findViewById(R.id.cl_pingjia_ratingbar);
            holder.studentName= (TextView) convertView.findViewById(R.id.cl_student_name);
            holder.ivAvatar = (CircleImageView) convertView.findViewById(R.id.cl_pingjia_touxiang);
            holder.ivCover = (ImageView) convertView.findViewById(R.id.cl_pingjia_cover);
            holder.gridView= (NoScrollPFGridView) convertView.findViewById(R.id.cl_gridview);
            convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }
        int id = data.get(position).getId();
        String ts = data.get(position).getTs();
        holder.date.setText(ts.substring(0, ts.indexOf(" ")));  //日期
        holder.content.setText(data.get(position).getTitle());  //内容
        holder.studentName.setText(data.get(position).getMemname());  //学生姓名
        holder.ratingBar.setRating(data.get(position).getScore());  //分数

        //头像
        if (data.get(position).getAvatar()!=null&&!data.get(position).getAvatar().equals("")){
            String avatar = data.get(position).getAvatar();
            com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(avatar,holder.ivAvatar);
        }
        //封面
        if (data.get(position).getCoverpic()!=null&&!data.get(position).getCoverpic().equals("")){
            String coverpic = data.get(position).getCoverpic();
            com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(coverpic, holder.ivCover);
        }
        //多图设置
        holder.gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
        holder.gridView.setTag(id);
        boolean empty = data.get(position).getPics().isEmpty();
        if (empty == false) {
            if (holder.gridView.getTag()!=null&&holder.gridView.getTag().equals(id)){

                String pics = data.get(position).getPics();
                final String[] split = pics.split(",");
                if (!pics.equals("")){
                    if (pics.indexOf(",")==-1) {
                        split[0]=pics;
                        holder.gridView.setVisibility(View.VISIBLE);
                        holder.gridView.setAdapter(new PJGridAdapter(split, context));
                        holder.gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                imageBrower(position, split);
                            }
                        });
                    }else{
                        holder.gridView.setVisibility(View.VISIBLE);
                        holder.gridView.setAdapter(new PJGridAdapter(split, context));
                        holder.gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                imageBrower(position, split);
                            }
                        });
                    }
                }else{
                    holder.gridView.setVisibility(View.GONE);
                }
            }

        }else{
            holder.gridView.setVisibility(View.GONE);
        }

        return convertView;
    }

    private void imageBrower(int position, String[] pics) {
        try {
            Intent intent = new Intent(context, ImagePagerActivity.class);
            intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
            intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, pics);
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private class ViewHolder{
        CircleImageView ivAvatar;
        ImageView ivCover;
        TextView studentName,content,date;
        RatingBar ratingBar;
        NoScrollPFGridView gridView;
    }
}
